package com.sam.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sam.entity.CouponEntity;
import com.sam.jifenbao.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ValidCouponListAdapter extends BaseAdapter{
    private Context context;
    private List<CouponEntity> couponEntityList;

    public ValidCouponListAdapter(Context context, List<CouponEntity> couponEntityList){
        this.context = context;
        this.couponEntityList = couponEntityList;
    }

    @Override
    public int getCount() {
        return couponEntityList.size();
    }

    @Override
    public CouponEntity getItem(int position) {
        return couponEntityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CouponEntity couponEntity = couponEntityList.get(position);
        ViewHolder viewHolder = null;
        if(convertView == null){
            convertView = View.inflate(context,R.layout.valid_coupon_listview_item,null);
            viewHolder = new ViewHolder();
            viewHolder.couponValueTV = (TextView) convertView.findViewById(R.id.coupon_value);
            viewHolder.couponNameTV = (TextView) convertView.findViewById(R.id.coupon_name);
            viewHolder.couponRuleTV = (TextView) convertView.findViewById(R.id.coupon_rule);
            viewHolder.couponValidTimeTV = (TextView) convertView.findViewById(R.id.coupon_valid_time);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.couponValueTV.setText(couponEntity.getCouponValue()+"");
        viewHolder.couponNameTV.setText(couponEntity.getCouponName());
        viewHolder.couponRuleTV.setText(String.format(context.getString(R.string.coupon_use_rule_format),couponEntity.getAtLeastValue()));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String validStartTime = dateFormat.format(new Date(couponEntity.getValidStartTime()));
        String validEndTime = dateFormat.format(new Date(couponEntity.getValidEndTime()));
        viewHolder.couponValidTimeTV.setText(String.format(context.getString(R.string.coupon_valid_date_format),validStartTime,validEndTime));
        return convertView;
    }

    private class ViewHolder {
        TextView couponValueTV,couponNameTV,couponRuleTV,couponValidTimeTV;
    }

}
